const app = getApp()
import {
  getUserInfo,
  login,
  editUserInfo,
 } from '../../api/user'
import QRCode from '../../utils/weapp-qrcode' // es6的方式
Page({
  data: {
    balance: 0,
    freeze: 0,
    score: 0,
    canWithDraw: 0,
    canWithDraw1: 0,
    score_sign_continuous: 0,
    ci: 0,
    tabClass: ["", "", "", "", ""],
    headImg: '',
    nickName: '',
    lv: 0,
    dealerDetail: null,
    sharePic: '',
    popShow: false,
    hideShopPopup: true,
    standardList: [],
    standardPrice: 0,
    standId: 0,
    hasSharePic: false,
    canProcess: false,
    service: '',
    hasTel: false,
    imgList: {},
    userInfo: null,
    kan: 0
  },
  checkStard() {
    let that = this
    let standardList = that.data.standardList
    var standardPrice = that.data.standardPrice
    standardList.forEach(item => {
      item.active = false
    })
    standardList[0].active = true
    standardPrice = standardList[0].Price * standardList[0].MinBuy
    that.setData({
      hideShopPopup: false,
      standardPrice,
      standardList,
    })
  },
  labelItemTap(e) {
    var that = this
    let standardList = that.data.standardList
    let i = e.currentTarget.dataset.propertyindex
    let standId = e.currentTarget.dataset.propertyid
    standardList.forEach(item => {
      item.active = false
    })
    standardList[i].active = true
    that.setData({
      standardPrice: standardList[i].Price * standardList[i].MinBuy,
      standardList,
      standId
    })
  },
  goCoupon() {
    if (this.data.hasTel) {
      wx.navigateTo({
        url: '/pages/coupons/coupons2',
      })
    } else {
      wx.showModal({
        content: '您还没有绑定手机号，请先绑定手机号再申领优惠券。',
        confirmText: '去绑定',
        success(e) {
          console.log(e);
          if (e.confirm) {
            wx.navigateTo({
              url: '/pages/my/info',
            })
          }
        },
      })
    }
  },
  onLoad: function (e) {
    wx.hideShareMenu()
    this.setData({
      imgList: app.globalData.imgList,
    })
    if( e.kan == 1 ){
      this.setData({
        kan: 1
      })
    }else{
      this.setData({
        kan: 0
      })
    }
  },
  onShareAppMessage(id) {
    // console.log(`/pages/index/index?id=${app.globalData.uid}&standId=${this.data.standId}`)
    return {
      title: '祥康酱酒',
      path: `/pages/index/index?id=${app.globalData.dealerInfo.id}`,
      imageUrl: 'https://mallcdn.oss-cn-beijing.aliyuncs.com/15920383876203520.jpg'
    }
  },

  closePopupTap() {
    this.setData({
      hideShopPopup: true
    })
  },
  nowShare() {
    this.setData({
      hideShopPopup: true
    })
  },
  showPop() {
    this.setData({
      popShow: !this.data.popShow
    })
  },
  notDealer() {
    if (this.data.dealerDetail == null) {
      wx.showToast({
        title: '请先申请为分销商！',
        icon: 'none'
      })
      return
    } else if (this.data.dealerDetail.IsValid == 0 && this.data.dealerDetail.IsValid != 1) {
      wx.showToast({
        title: '请等待经销商身份审核',
        icon: 'none'
      })
      return
    }
  },
  async getPhoneNumber(e) {
    let encryptedData = e.detail.encryptedData
		let iv = e.detail.iv
		wx.login({
			success:async(res) => {				

		let loginData = await login(wx.getStorageSync('uuid'), encryptedData, iv,res.code)
			console.log(loginData);
			if (loginData.code == 10000) {
				wx.showToast({
					title: '登录成功'
				})
				wx.setStorageSync('access_token', loginData.data.accessToken)
				wx.setStorageSync('refresh_token', loginData.data.refreshToken)
				console.log(loginData.data.accessToken);
				let userInfo = await getUserInfo(loginData.data.accessToken)
				console.log(userInfo)
				app.globalData.userInfo = userInfo.data
				this.setData({
					userInfo: userInfo.data,
					headImg: userInfo.data.face
				})
				if( this.data.kan == 1 ){
					wx.navigateBack({
						delta: 1 //返回上一级页面
					})
				}
			}
			},
		})
  },
  goTodealer() {
    // wx.showToast({
    //   title: '敬请期待',
    //   icon: 'none'
    // })
    // return
  },
  goProcess() {
    // wx.showToast({
    //   title: '敬请期待',
    //   icon: 'none'
    // })
    // return
    if (!app.globalData.dealerInfo || app.globalData.dealerInfo.distributionStatus != 'PASS') {
      wx.showToast({
        title: '您还不是经销商',
        icon: 'none'
      })
      return
    }
    wx.navigateTo({
      url: '/pages/d-center/list',
    })
    // if(this.data.canProcess){

    // }else{
    //   wx.showToast({
    //     title: '您还不是顶级经销商',
    //     icon:'none'
    //   })
    // }
  },
  goToCenter() {
    if (this.data.dealerDetail == null) {
      wx.showToast({
        title: '请先申请为分销商！',
        icon: 'none'
      })
      return
    } else if (this.data.dealerDetail.IsValid == 0) {
      wx.showToast({
        title: '请等待经销商身份审核',
        icon: 'none'
      })
      return
    } else {
      wx.navigateTo({
        url: '/pages/d-center/list',
      })
    }
  },
  clearCache() {
    wx.showModal({
      title: '警告',
      content: '是否要清除缓存？',
      success: (res) => {
        if (res.confirm) {
          wx.showLoading({
            title: '清理中...',
          })
          setTimeout(function () {
            wx.clearStorage({
              success: res => {
                wx.showToast({
                  title: '清理完毕',
                  icon: 'success',
                  duration: 1000,
                  success: () => {
                    setTimeout(function () {
                      wx.switchTab({
                        url: '/pages/index/index'
                        //url同app.json中配置的tabBar路径
                      })
                    }, 1000)
                  }
                })

              }
            })
            wx.hideLoading()
          }, 1000)


        }
      }
    })
  },
  changeInfo() {
    let that = this
    if (!wx.getStorageSync('access_token')) {
      return
    }
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        editUserInfo(wx.getStorageSync('access_token'), res.userInfo.nickName, res.userInfo.avatarUrl, 1).then(res => {
          this.setData({
            userInfo: res.data,
            headImg: res.data.face
          })
          app.globalData.userInfo = res.data
        })
      }
    })
  },
  shareBtn() {
    wx.showToast({
      title: this.data.dealerInfo ? '请等待经销商审核' : '请先申请为经销商',
      icon: 'none'
    })
  },
  withDraw() {
    if (this.data.dealerDetail == null) {
      wx.showToast({
        title: '请先申请为经销商！',
        icon: 'none'
      })
      return
    } else if (this.data.dealerDetail.IsValid == 0) {
      wx.showToast({
        title: '请等待经销商身份审核',
        icon: 'none'
      })
      return
    }
    wx.navigateTo({
      url: '/pages/withdraw/withdraw',
    })
  },
  goCount() {
    wx.showToast({
      title: '敬请期待',
      icon: 'none'
    })
  },
  async onShow() {
    if (wx.getStorageSync('access_token')) {
      console.log(app.globalData.userInfo)
      this.setData({
        userInfo: app.globalData.userInfo,
        headImg: app.globalData.userInfo.face,
        dealerInfo: app.globalData.dealerInfo
      })
    }
  },

  toTel() {
    wx.makePhoneCall({
      phoneNumber: '13844241140',
    })
  },
  goInfo() {
    wx.navigateTo({
      url: '/pages/my/info',
    })
  },
  getUserInfo: function (cb) {
    var that = this
    wx.login({
      success: function () {
        wx.getUserInfo({
          success: function (res) {
            that.setData({
              userInfo: res.userInfo
            });
            that.globalData.userInfo = res.data
          }
        })
      }
    })
  },
  relogin: function () {
    var that = this;
    wx.authorize({
      scope: 'scope.userInfo',
      success() {
        app.globalData.token = null;
        app.login();
        wx.showModal({
          title: '提示',
          content: '重新登陆成功',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              that.onShow();
            }
          }
        })
      },
      fail(res) {
        //console.log(res);
        wx.openSetting({});
      }
    })
  },
  score: function () {
    wx.navigateTo({
      url: "/pages/score/score"
    })
  },
})